package com.itboye.sunsun_china.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Spanned;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.itboye.sunsun_china.www.aq.base.MyApplication;

/**
 * 字符串转表情Spanned 表情格式 [:f000}
 * 
 * @author Young
 * 
 */
public class String2Spanned extends TypeAdapter<Spanned> {

	static Pattern pattern;

	static Map<String, Integer> map = new HashMap<>();

	static ImageGetter imageGetter = new ImageGetter() {
		@Override
		public Drawable getDrawable(String source) {
			int id = Integer.parseInt(source);
			Drawable d = MyApplication.context.getResources().getDrawable(id);
			d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			return d;
		}
	};

	static {

		pattern = Pattern.compile("\\[:f\\d{3}\\}");

	}

	@Override
	public Spanned read(JsonReader in) throws IOException {
		if (in.peek() == JsonToken.NULL) {
			in.nextNull();
			return null;
		}

		String origStr = in.nextString();
		Matcher matcher = pattern.matcher(origStr);

		StringBuilder stringBuilder = new StringBuilder();

		int last = 0;
		while (matcher.find()) {

			int s = matcher.start();
			int e = matcher.end();
			stringBuilder.append(origStr.substring(last, s));

			String group = matcher.group();
			Integer emojId = map.get(group);
			if (emojId == null) {
				stringBuilder.append(group);
			} else {
				stringBuilder.append("<img src='");
				stringBuilder.append(emojId);
				stringBuilder.append("'/>");
			}

			last = e;

		}
		stringBuilder.append(origStr.substring(last, origStr.length()));
		System.out.println("Spanner" + stringBuilder.toString());
		return Html.fromHtml(
				stringBuilder.toString().replace("\n", "<br>")
						.replace("&lt;/p&gt;", "<br>")
						.replace("&lt;p dir=&quot;ltr&quot;&gt;", "<br>")
						.replace("&lt;", "<").replace("&gt;", ">"),
				imageGetter, null);// ,
									// imageGetter,
									// null);
	}

	@Override
	public void write(JsonWriter arg0, Spanned arg1) throws IOException {

	}

	public static CharSequence parsed(String content) {
		if (content == null) {
			// in.nextNull();
			return null;
		}

		// String origStr=content.nextString();
		Matcher matcher = pattern.matcher(content);

		StringBuilder stringBuilder = new StringBuilder();

		int last = 0;
		while (matcher.find()) {

			int s = matcher.start();
			int e = matcher.end();
			stringBuilder.append(content.substring(last, s));

			String group = matcher.group();
			Integer emojId = map.get(group);
			if (emojId == null) {
				stringBuilder.append(group);
			} else {
				stringBuilder.append("<img src='");
				stringBuilder.append(emojId);
				stringBuilder.append("'/>");
			}

			last = e;

		}
		stringBuilder.append(content.substring(last, content.length()));
		System.out.println("Spanner" + stringBuilder.toString());
		return Html.fromHtml(
				stringBuilder.toString().replace("\n", "<br>")
						.replace("&lt;/p&gt;", "<br>")
						.replace("&lt;p dir=&quot;ltr&quot;&gt;", "<br>")
						.replace("&lt;u&gt;", "<u>")
						.replace("&lt;/u&gt;", "</u>"), imageGetter, null);// ,
																			// imageGetter,
																			// null);
	}

}
